home *** CD-ROM | disk | FTP | other *** search
- #include "header.h"
-
- extern DirectDraw *DD;
- int Reject(POLYGON *p);
- BYTE Darkness[33][256];
- BYTE GVects[256][256];
-
- WORLD::WORLD()
- {
- FILE *fp;
- UpdateMatrix = new MATRIX;
- ImageMap = (BYTE *)GlobalAlloc(GPTR, sizeof(BYTE) * 256 * 256);
- fp = fopen("dark.dat", "rb");
- fread(Darkness, sizeof(BYTE), 33 << 8, fp);
- fclose(fp);
- fp = fopen("gvects.vec", "rb");
- fread(GVects, sizeof(BYTE), 65536, fp);
- fclose(fp);
- };
-
- WORLD::~WORLD()
- {
- GlobalFree((BYTE *)ImageMap);
- delete UpdateMatrix;
- };
-
- int WORLD::FreeWorld()
- {
- int i, j;
-
- delete PolyList;
- for(i = 0; i < Number_LandScapes; i++)
- delete LandScape[i].Vertex;
- for(i = 0; i < Number_Textures; i++)
- Texture[i].Free();
- // delete BitmapObj;
- delete Texture;
- delete Camera;
- return 0;
- };
-
- // Optimize in assembly
- int WORLD::ProjectWorld(long Perspective)
- {
- int i, j, p;
- float d, r;
- int sx, ex;
- int sz, ez;
-
- for(i = 0; i < Number_LandScapes; i++)
- {
- sx = Camera[Current_Camera].xTrans;
- sz = Camera[Current_Camera].zTrans;
- sx >>= 10;
- sz >>= 10;
- ex = ( ((sx + X_DIR) < LandScape[i].Width) ? (sx + X_DIR) : (LandScape[i].Width - 1));
- sx = ( ((sx - X_DIR) < 0) ? (0) : (sx - X_DIR));
- ez = ( ((sz + Z_DIR) < LandScape[i].Height) ? (sz + Z_DIR) : (LandScape[i].Height - 1));
- sz = ( ((sz - Z_DIR) < 0) ? (0) : (sz - Z_DIR));
-
- for(int v = sx; v < ex; v++)
- {
- for(int w = sz; w < ez; w++)
- {
- p = (w << 8) + v;
- r = LandScape[i].Vertex[p].vz;
- if(r <= 0.0)
- continue;
- d = (float)PERSPECTIVE / (float)r;
- LandScape[i].Vertex[p].sx = (long)(0.5 + LandScape[i].Vertex[p].vx * d + DD->HalfScreen.x);
- LandScape[i].Vertex[p].sy = (long)(0.5 + LandScape[i].Vertex[p].vy * d + DD->HalfScreen.y);
- }
- }
- }
-
- return 0;
- };
-
- int WORLD::BackfaceCull(POLYGON *p)
- {
- float X1, Y1, Z1;
- float X2, Y2, Z2;
- float X3, Y3, Z3;
- float D;
-
- X1 = p->Vertex[0].vx; Y1 = p->Vertex[0].vy; Z1 = p->Vertex[0].vz;
- X2 = p->Vertex[1].vx; Y2 = p->Vertex[1].vy; Z2 = p->Vertex[1].vz;
- X3 = p->Vertex[2].vx; Y3 = p->Vertex[2].vy; Z3 = p->Vertex[2].vz;
-
- D = (X3 * ((Z1 * Y2) - (Y1 * Z2)))
- + (Y3 * ((X1 * Z2) - (Z1 * X2)))
- + (Z3 * ((Y1 * X2) - (X1 * Y2)));
-
- return(D < 0);
- };
-
- int WORLD::DrawWorld(void *Screen)
- {
- int i;
-
- for(i = 0; i < Number_LandScapes; i++)
- DrawLandScape(&LandScape[i], Screen);
- return 0;
- };
-
- int WORLD::DrawLandScape(LANDSCAPE *l, void *Screen)
- {
- int i, j;
- int r = 0;
- int sort = 1;
- int sx, ex;
- int sz, ez;
-
- sx = Camera[Current_Camera].xTrans;
- sz = Camera[Current_Camera].zTrans;
- sx >>= 10;
- sz >>= 10;
- ex = ( ((sx + (X_DIR - 1)) < (l->Width - 1)) ? (sx + (X_DIR - 1)) : ((l->Width - 2)));
- sx = ( ((sx - (X_DIR - 1)) < 0) ? (0) : (sx - (X_DIR - 1)));
- ez = ( ((sz + (Z_DIR - 1)) < (l->Height - 1)) ? (sz + (Z_DIR - 1)) : ((l->Height - 2)));
- sz = ( ((sz - (Z_DIR - 1)) < 0) ? (0) : (sz - (Z_DIR - 1)));
-
- Number_Polygons = 0;
- for(i = sx; i < ex; i++)
- for(int k = sz; k < ez; k++)
- {
- PolyList[Number_Polygons].Vertex[0] = l->Vertex[(k << 8) + i];
- PolyList[Number_Polygons].Vertex[1] = l->Vertex[(k << 8) + i + 1];
- PolyList[Number_Polygons].Vertex[2] = l->Vertex[((k + 1) << 8) + i];
- PolyList[Number_Polygons].Number_Vertices = 3;
- PolyList[Number_Polygons].TextPt[0].x = 0;
- PolyList[Number_Polygons].TextPt[0].y = 0;
- PolyList[Number_Polygons].TextPt[1].x = 31;
- PolyList[Number_Polygons].TextPt[1].y = 0;
- PolyList[Number_Polygons].TextPt[2].x = 0;
- PolyList[Number_Polygons].TextPt[2].y = 31;
- PolyList[Number_Polygons].Texture = *(ImageMap + (k << 8) + i);
- if(!Reject(&PolyList[Number_Polygons]))
- {
- if(!BackfaceCull(&PolyList[Number_Polygons]))
- {
- for(j = 0; j < PolyList[Number_Polygons].Number_Vertices; j++)
- {
- if(PolyList[Number_Polygons].Vertex[j].vz <= HITHER)
- {
- PolyList[Number_Polygons].Number_Vertices = 1;
- // zClipPolygon(&PolyList[Number_Polygons]);
- break;
- }
- }
- if(PolyList[Number_Polygons].Number_Vertices >= 3)
- {
- PolyList[Number_Polygons].Color = 0;
- Number_Polygons++;
- }
- }
- }
-
- PolyList[Number_Polygons].Vertex[0] = l->Vertex[(k << 8) + i + 1];
- PolyList[Number_Polygons].Vertex[1] = l->Vertex[((k + 1) << 8) + i + 1];
- PolyList[Number_Polygons].Vertex[2] = l->Vertex[((k + 1) << 8) + i];
- PolyList[Number_Polygons].Number_Vertices = 3;
- PolyList[Number_Polygons].TextPt[0].x = 31;
- PolyList[Number_Polygons].TextPt[0].y = 0;
- PolyList[Number_Polygons].TextPt[1].x = 31;
- PolyList[Number_Polygons].TextPt[1].y = 31;
- PolyList[Number_Polygons].TextPt[2].x = 0;
- PolyList[Number_Polygons].TextPt[2].y = 31;
- PolyList[Number_Polygons].Texture = *(ImageMap + (k << 8) + i);
- if(!Reject(&PolyList[Number_Polygons]))
- {
- if(!BackfaceCull(&PolyList[Number_Polygons]))
- {
- for(j = 0; j < PolyList[Number_Polygons].Number_Vertices; j++)
- {
- if(PolyList[Number_Polygons].Vertex[j].vz <= HITHER)
- {
- PolyList[Number_Polygons].Number_Vertices = 1;
- // zClipPolygon(&PolyList[Number_Polygons]);
- break;
- }
- }
- if(PolyList[Number_Polygons].Number_Vertices >= 3)
- {
- PolyList[Number_Polygons].Color = 0;
- Number_Polygons++;
- }
- }
- }
- }
-
- if(Number_Polygons <= 1)
- return(1);
- SortPolyList(PolyList, 0, Number_Polygons - 1);
-
- for(i = 0; i < Number_Polygons; i++)
- {
- if(PolyList[i].Number_Vertices == 3)
- DrawTriangle(&PolyList[i], Screen);
- // else
- // TexturedPolygon(&PolyList[i], Screen, FALSE);
- }
- return 0;
- };
-
- int WORLD::SortPolyList(POLYGON *item, int left, int right)
- {
- int i, j;
- float x;
- POLYGON y;
-
- i = left; j = right;
- x = item[(left + right) >> 1].Vertex[0].vz;
-
- do
- {
- while(item[i].Vertex[0].vz > x && i < right) i++;
- while(x > item[j].Vertex[0].vz && j > left) j--;
-
- if(i <= j)
- {
- y = item[i];
- item[i] = item[j];
- item[j] = y;
- i++; j--;
- }
-
- } while(i <= j);
-
- if(left < j)
- SortPolyList(item, left, j);
- if(i < right)
- SortPolyList(item, i, right);
- return 0;
- };
-
- int WORLD::UpdateWorld()
- {
- int i;
- int sx, ex;
- int sz, ez;
- int dx, dz;
- int p1, p2, p3, p4;
- int xrem, zrem;
- VECTOR a, b, c;
- float cosa, sina;
-
- sx = Camera[Current_Camera].xTrans;
- sz = Camera[Current_Camera].zTrans;
-
- xrem = sx & 1023;
- zrem = sz & 1023;
-
- sx >>= 10;
- sz >>= 10;
-
- if( Camera[Current_Camera].yAngle >= -1.57 &&
- Camera[Current_Camera].yAngle <= 1.57 )
- ex = sx + 1;
- else
- ex = sx - 1;
- if( Camera[Current_Camera].yAngle >= 0 &&
- Camera[Current_Camera].yAngle <= 3.16 )
- ez = sz + 1;
- else
- ez = sz - 1;
-
- sx = (sx < 0) ? 0 : (sx > 255) ? 255 : sx;
- ex = (ex < 0) ? 0 : (ex > 255) ? 255 : ex;
- sz = (sz < 0) ? 0 : (sz > 255) ? 255 : sz;
- ez = (ez < 0) ? 0 : (ez > 255) ? 255 : ez;
-
- p1 = (sz << 8) + sx;
- p2 = (sz << 8) + ex;
- p3 = (ez << 8) + sx;
- p4 = (ez << 8) + ex;
-
- p1 = LandScape[0].Vertex[p1].oy;
- p2 = LandScape[0].Vertex[p2].oy;
- p3 = LandScape[0].Vertex[p3].oy;
- p4 = LandScape[0].Vertex[p4].oy;
-
- if(p1 < p2)
- dz = p1;
- else
- dz = p2;
- if(p3 < p4)
- dx = p3;
- else
- dx = p4;
- if(dz > dx)
- dz = dx;
-
- Camera[Current_Camera].yTrans = dz - 640;
-
- UpdateMatrix->Identity();
- UpdateMatrix->Scale(1, 1, 1);
- UpdateMatrix->Translate(-Camera[Current_Camera].xTrans,
- -Camera[Current_Camera].yTrans,
- -Camera[Current_Camera].zTrans);
- UpdateMatrix->Rotate(-Camera[Current_Camera].xAngle,
- -Camera[Current_Camera].yAngle,
- -Camera[Current_Camera].zAngle);
-
- for(i = 0; i < Number_LandScapes; i++)
- UpdateMatrix->TransformWithCamera(&LandScape[i]);
- return 0;
- };
-
- int Reject(POLYGON *p)
- {
- int l, r, u, d, z;
- int n;
-
- l = r = u = d = 0;
- n = p->Number_Vertices;
- for(int i = 0; i < n; i++)
- {
- if(p->Vertex[i].sx < 0)
- l++;
- if(p->Vertex[i].sx >= DD->Screen.x)
- r++;
- if(p->Vertex[i].sy < 0)
- u++;
- if(p->Vertex[i].sy >= DD->Screen.y)
- d++;
- if(p->Vertex[i].vz < HITHER)
- z++;
- }
-
- if(l == n || r == n || u == n || d == n || z == n)
- return 1;
- else
- return 0;
- };
-